<?php
namespace plugins\module_config\controller;

use app\common\lib\user_operate\ModuleOperate as MO; //引入此类
use app\common\lib\user_operate\PluginOperate as PO;
use cmf\controller\PluginAdminBaseController;
use plugins\module_config\model\PluginApiIndexModel as Model;

/**
 * 数据表配置管理控制器
 */
class PluginListController extends PluginAdminBaseController
{
    protected function _initialize()
    {
        parent::_initialize();
        $this->Model = new Model();
        $adminId     = cmf_get_current_admin_id(); //获取后台管理员id，可判断是否登录
        if (!empty($adminId)) {
            $this->assign("admin_id", $adminId);
        }
    }
    /**
     * 插件列表
     */
    public function index()
    {
        $data = $this->request->param();
        if (!empty($data['name_storeid'])) {
            $name_storeid     = $data['name_storeid'];
            $data['name']     = explode('|', $name_storeid)[0];
            $data['store_id'] = explode('|', $name_storeid)[1];
        }
        //模块列表
        $mo          = new MO();
        $module_list = $mo->getModuleList();
        if ($module_list['status'] != 'success') {
            $list = [];
        } else {
            $list = $module_list['data'];
        }
        $this->assign('module_list', $list);
        //模块配置文件列表
        $file_list = $this->Model->getModuleTags($data['name']);
        $this->assign('file_list', $file_list);
        $po = new PO();
        //获取插件列表
        $plugin_list = $po->getPluginList($data['store_id']);
        if ($plugin_list['status'] != 'success') {
            $list = [];
        } else {
            $list = $plugin_list['data'];
        }
        //获取插件安装状态
        foreach ($list as $key => $value) {
            $res = $po->isInstall($data['name'], $value['name']);
            if (true === $res) {
                $value['is_install'] = 'true';
            } else {
                $value['is_install'] = 'false';
            }
            $list[$key] = $value;
        }
        $data['file_name'] = !empty($data['file_name']) ? $data['file_name'] : '';
        $this->assign('data', $data);
        $this->assign('list', $list);
        return $this->fetch();
    }
    /**
     * 安装插件
     */
    public function install()
    {
        $data = $this->request->param();
        $this->assign('data', $data);
        return $this->fetch();
    }
    /**
     * 文档
     */
    public function helpFile()
    {
    }
}
